/*
 *
 * Visualizador tablas a partir de datos JSON
 * author: Carlos Gómez (carlosg@tid.es)
 */
var nNormalTables=0;
var nSumarizeTables=0;

function sumarizeMetrics(allData){
    var docHTML = "";
    docHTML+=addHTMLElement("h1","Resumen métricas 10-09-2010");
    docHTML+=addHTMLElement("p","QACard tarari tararo....");
    var tableHeader=addHTMLElement("thead",addRow("head",["Metric","Initiative Value","Company Value"]));
    var tableBody=""
    for (var i=0;i<allData.sections.length;i++){
        var head=allData.sections[i].head;
        var lastFile=allData.sections[i].data.length;
        var data=allData.sections[i].data[allData.sections[i].data.length-1];
        var sumarize=allData.sections[i].sumarize;
        for (var j=1;j<head.length;j++){
            if (sumarize==null){
                tableBody+=addRow("body",[head[j],data[j]]);
            } else {
                tableBody+=addRow("body",[head[j],data[j],sumarize[0][j]]);
            }
        }

    }
    tableBody=addHTMLElement("tbody",tableBody);
    docHTML+=addHTMLElement("table id=\"styling-custom-striping"+(nNormalTables++)+"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"",tableHeader+tableBody);
    return docHTML;
}

$(function(){
    var json=eval(data);
    var docHTML = sumarizeMetrics(json);
    docHTML+=addHTMLElement("h2","Comparativa con las QACards anteriores y los valores medios de la empresa");
    for (var i=0;i<json.sections.length;i++){
        docHTML+=addHTMLElement("h2",json.sections[i].title);
        docHTML+=addHTMLElement("p",json.sections[i].description);
        // Cabecera de la tabla
        var head=json.sections[i].head;
        var tableHeader ="";
        if (head.length > 0){
            tableHeader=addHTMLElement("thead",addRow("head",head));
        }
        var dades=json.sections[i].data;
        var tableBody=drawTableBody(dades);
        if ( (tableBody != "") && (tableHeader != "") ){
            docHTML+=addHTMLElement("table id=\"styling-custom-striping"+(nNormalTables++)+"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"",tableHeader+tableBody);
        }
        var sumarize=json.sections[i].sumarize;
        if (sumarize != null){
            tableBody=drawTableBody(sumarize);
            if ((tableBody!="")){
                docHTML+=addHTMLElement("table id=\"styling-resume-striping"+(nSumarizeTables++)+"\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"",tableBody);
            }
        }
       
    }
    document.body.innerHTML = docHTML;
}

)
$(function() {
    var json=eval(data);
    for (var i=0;i<nNormalTables;i++){
        $("#styling-custom-striping"+i).tableSorter({
            sortColumn: 'name',			// Integer or String of the name of the column to sort by.
            sortClassAsc: 'headerSortUp', 		// class name for ascending sorting action to header
            sortClassDesc: 'headerSortDown', 	// class name for descending sorting action to header
            headerClass: 'header', 			// class name for headers (th's)
            stripingRowClass: ['even','odd'],	// class names for striping supplyed as a array.
            stripeRowsOnStartUp: true,
            disableHeader: 0

        });
    }
    for (var i=0;i<nSumarizeTables;i++){
        $("#styling-resume -striping"+i).tableSorter({
            sortColumn: 'name',			// Integer or String of the name of the column to sort by.
            sortClassAsc: 'headerSortUp', 		// class name for ascending sorting action to header
            sortClassDesc: 'headerSortDown', 	// class name for descending sorting action to header
            headerClass: 'header', 			// class name for headers (th's)
            stripingRowClass: ['even','odd'],	// class names for striping supplyed as a array.
            stripeRowsOnStartUp: true,
            disableHeader: 0
        });
    }
});

